package cn.com.shopec.erp.system.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.com.shopec.erp.base.vo.SupplierVo;
import cn.com.shopec.erp.common.utils.SpringUtil;
import cn.com.shopec.erp.system.constant.SystemConstant;
import cn.com.shopec.erp.system.service.IRoleService;
import cn.com.shopec.erp.system.vo.RoleVo;
import cn.com.shopec.erp.system.vo.UserVo;

/** 
 * @ClassName: UserUtils 
 * @Description: 用户相关的utils类
 * @author: lule
 * @date 2013-5-25 下午5:33:32
 *  
 */

public class UserUtils {
	
	/** 
	 * @Title: isPurchaseMan 
	 * @Description: 是否采购员
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:34:40
	 */
	public static boolean isPurchaseMan() throws Exception {
		return checkAuidtRole(SystemConstant.PURCHASE_MAN);
	}
	
	/** 
	 * @Title: isPurchaseSupervisor 
	 * @Description: 是否采购主管
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:35:58
	 */
	public static boolean isPurchaseSupervisor() throws Exception {
		return checkAuidtRole(SystemConstant.PURCHASE_SUPERVISOR);
	}
	
	/** 
	 * @Title: isPurchaseDirector 
	 * @Description: 是否采购总监
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:36:29
	 */
	public static boolean isPurchaseDirector() throws Exception {
		return checkAuidtRole(SystemConstant.PURCHASE_DIRECTOR);
	}
	
	/** 
	 * @Title: isOperationSupervisor 
	 * @Description: 是否采购总监
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:36:57
	 */
	public static boolean isOperationSupervisor() throws Exception {
		return checkAuidtRole(SystemConstant.OPERATION_SUPERVISOR);
	}
	
	/** 
	 * @Title: isFinancialPrincipal 
	 * @Description: 是否财务负责人
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:37:48
	 */
	public static boolean isFinancialPrincipal() throws Exception {
		return checkAuidtRole(SystemConstant.FINANCIAL_PRINCIPAL);
	}
	
	/** 
	 * @Title: isGeneralManager 
	 * @Description: 是否总经理
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:38:11
	 */
	public static boolean isGeneralManager() throws Exception {
		return checkAuidtRole(SystemConstant.GENERAL_MANAGER);
	}
	
	/**
	 * 
	 * @MethodName isWarehouseSupervisor
	 * @Description 是否是仓库主管
	 * @author chengui
	 * @date 2013-6-6 下午2:22:56
	 * @return
	 * @throws Exception
	 */
	public static boolean isWarehouseSupervisor() throws Exception {
		return checkAuidtRole(SystemConstant.WAREHOUSE_SUPERVISOR);
	}
	
	/** 
	 * @Title: isPoAuditor 
	 * @Description: 是否采购单的审核人(可能是采购主管、采购总监、运营主管、财务负责人、总经理之一)
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午6:32:01
	 */
	public static boolean isPoAuditor() throws Exception {
		return isPurchaseMan() || isPurchaseSupervisor() || isPurchaseDirector() || isOperationSupervisor() || isFinancialPrincipal() || isGeneralManager();
	}
	
	/** 
	 * @Title: getLoginUserName 
	 * @Description: 获得当前登录用户的用户名
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:39:41
	 */
	public static String getLoginUserName() {
		UserVo userVo = (UserVo) ActionContext.getSessionAttribute(SystemConstant.LOGINUSER_SESSION);
		if (null != userVo) {
			return userVo.getUserName();
		}
		return "";
	}
	
	/** 
	 * @Title: getLoginUserRealName 
	 * @Description: 获得当前登录用户的真实姓名
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:39:41
	 */
	public static String getLoginUserRealName() {
		UserVo userVo = (UserVo) ActionContext.getSessionAttribute(SystemConstant.LOGINUSER_SESSION);
		if (null != userVo) {
			return userVo.getRealName();
		}
		return "";
	}
	
	/** 
	 * @Title: getLoginUserPoAuditorRole 
	 * @Description: 获得当前登录用户的PO审核人身份（PO审核人身份不完全等于用户在本系统中的角色）
	 * @return  
	 * @throws 
	 * @author: lule
	 * @date: 2013-5-25下午5:45:45
	 */
	public static String[] getLoginUserPoAuditorRole() throws Exception {
		UserVo userVo = (UserVo) ActionContext.getSessionAttribute(SystemConstant.LOGINUSER_SESSION);
		List<RoleVo> roleVos = SpringUtil.getBean(IRoleService.class).getRoleVos(userVo);
		String[] roleCodes = null;
		if (null != roleVos && roleVos.size() > 0) {
			roleCodes = new String[roleVos.size()];
			for (int i=0; i<roleVos.size(); i++) {
				roleCodes[i] = roleVos.get(i).getRoleCode();
			}
		}
		return roleCodes;
	}
	
	/**
	 * 
	 * @MethodName getUserVo
	 * @Description 获得当前登录用户Vo
	 * @author chengui
	 * @date 2013-6-4 下午3:01:58
	 * @param request
	 * @return UserVo
	 */
	public static UserVo getUserVo(HttpServletRequest request) {
		HttpSession session = request.getSession();
		UserVo userVo = (UserVo) session.getAttribute(SystemConstant.LOGINUSER_SESSION);
		return userVo;
	}
	
	/**
	 * 
	 * @MethodName getUserVo
	 * @Description 获得当前登录用户Vo
	 * @author chengui
	 * @date 2013-6-6 下午2:00:33
	 * @return UserVo
	 */
	public static UserVo getUserVo() {
		UserVo userVo = (UserVo) ActionContext.getSessionAttribute(SystemConstant.LOGINUSER_SESSION);
		return userVo;
	}
	
	/**
	 * 
	 * @MethodName checkAuidtRole
	 * @Description 根据传进来的角色判断用户是否拥有该角色
	 * @author chengui
	 * @date 2013-6-6 上午11:35:30
	 * @param roleCode
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean checkAuidtRole(String roleCode) throws Exception {
		boolean result = false;
		UserVo userVo = (UserVo) ActionContext.getSessionAttribute(SystemConstant.LOGINUSER_SESSION);
		List<RoleVo> roleVos = SpringUtil.getBean(IRoleService.class).getRoleVos(userVo);
		if (null != roleVos && roleVos.size() > 0) {
			for (RoleVo roleVo : roleVos) {
				if (roleVo.getRoleCode().equals(roleCode)) {
					result = true;
				}
			}
		}
		return result;
	}
	
	/**
	 * 
	 * @MethodName getSuppVo
	 * @Description 获得供应商登录账号信息
	 * @author chengui
	 * @date 2013-6-9 下午2:49:11
	 * @param request
	 * @return SupplierVo
	 */
	public static SupplierVo getSuppVo(HttpServletRequest request) {
		HttpSession session = request.getSession();
		SupplierVo suppVo = (SupplierVo) session.getAttribute(SystemConstant.LOGINSUPPLIER_SESSION);
		return suppVo;
	}
	
	/**
	 * 
	 * @MethodName getSuppVo
	 * @Description 获得供应商登录账号信息
	 * @author chengui
	 * @date 2013-6-9 下午5:46:23
	 * @return SupplierVo
	 */
	public static SupplierVo getSuppVo() {
		SupplierVo suppVo = (SupplierVo) ActionContext.getSessionAttribute(SystemConstant.LOGINSUPPLIER_SESSION);
		return suppVo;
	}
}
